package com.work.customer.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.work.customer.dao.CustUserMapper;
import com.work.customer.dao.RoleMapper;
import com.work.customer.pojo.CustUser;
import com.work.customer.pojo.Role;
import com.work.customer.service.RoleService;
import com.work.user.feign.RoleFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private CustUserMapper custUserMapper;

    //给用户添加角色
    @Override
    public void add(Integer uid,Integer[] rids) {
        if(rids.length>0){
            //向用户角色中间表添加数据
            for (Integer rid : rids) {
                roleMapper.addByRid(uid,rid);
            }
        }

    }

    //修改用户角色
    @Override
    public void update(Integer uid,Integer[] rids) {
        //查询用户的所有角色id
        Integer[] rids1 = custUserMapper.findByUid(uid);
        if(rids.length>0){
            //查询所有角色.向用户角色中间表删除数据
           this.delete(uid,rids1);
            //重新添加角色
            this.add(uid,rids);
        }

    }

    //删除用户角色
    @Override
    public void delete(Integer uid,Integer[] rids) {
        if(rids.length>0){
            //向用户角色中间表删除数据
            for (Integer rid : rids) {
                roleMapper.deleteByUid(uid,rid);
            }
        }

    }

    @Override
    public Page<Role> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Role>)roleMapper.selectByExample(example);
    }

    //根据用户id查询角色
    @Override
    public List<Role> findById(Integer uid) {
        List<Role> list = new ArrayList<>();
        Integer[] rids = custUserMapper.findByUid(uid);
        for (Integer rid : rids) {
            Role role = roleMapper.selectByPrimaryKey(rid);
            list.add(role);
        }
        return list;
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(CustUser.class);
        Example.Criteria criteria = example.createCriteria();
            if (searchMap != null) {

                // 角色名
                if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                    criteria.andLike("name", "%" + searchMap.get("name") + "%");
                }

                // 角色分类
                if (searchMap.get("category") != null && !"".equals(searchMap.get("category"))) {
                    criteria.andLike("category", "%" + searchMap.get("category") + "%");
                }

            }
        return example;
    }

}
